#include <stdio.h>
#include <string.h>
int check(int x, char st[])
{
    if (x == 1)
        return 1;
    for (int i = 0; i < x - 1; i++)
    {
        if (st[i] == st[i + 1])
            return 0;
    }
    return 1;
}
void test(int n, int m, char s[1010], char t[110])
{
    if (m == 1)
    {
        int k = 1;
        for (int i = 0; i < n - 1; i++)
        {
            if (s[i] == s[i + 1])
            {
                if (t[0] == s[i])
                {
                    printf("NO\n");
                    k = 0;
                    break;
                }
            }
        }
        if (k == 1)
            printf("YES\n");
    }
    else
    {
        if (check(m, t) == 0)
            printf("NO\n");
        else
        {
            int flag = 1;
            for (int i = 0; i < n - 1; i++)
            {
                if (s[i] == s[i + 1])
                {
                    if (s[i] == t[0] || s[i + 1] == t[m - 1])
                    {
                        printf("NO\n");
                        flag = 0;
                        break;
                    }
                }
            }
            if (flag == 1)
                printf("YES\n");
        }
    }
}
int main()
{
    int t = 0;
    scanf("%d", &t);
    while (t--)
    {
        int n = 0, m = 0;
        scanf("%d%d", &n, &m);
        char s[10000];
        char t[110];
        getchar();
        scanf("%s", s);
        getchar();
        scanf("%s", t);
        if (check(n, s) == 1)
        {
            printf("YES\n");
        }
        else
        {
            test(n, m, s, t);
        }
    }
    return 0;
}